﻿using System;
using System.Threading;
using HotelManagerComm.CommHelper;
using Sunny.UI;
using System.Timers;
using HotelManagerUI.Comm;
using HotelManagerBLL.HotelBLL;
using HotelManagerModel.HotelModel;


namespace HotelManagerUI
{
    public partial class FrmLogin : UIForm
    {
        #region 获取图片验证码的变量

        private string imgCode = string.Empty;

        #endregion


        #region 时间字符串
        private string _TimesString = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmLogin()
        {
            InitializeComponent();
        }
        #endregion

        #region 窗体加载
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            TimeStart();
            // 加载验证码
            InitVerityCode();
            //StartTimer();
        }

        #endregion

        #region 时间的显示
        /// <summary>
        /// 开始时间
        /// </summary>
        private void TimeStart()
        {
            Text = CommConst.WelcomsTime + _TimesString;
            // 定义一个线程
            Thread thread = new Thread(new ThreadStart(GetTimes));
            thread.IsBackground = true;
            thread.Start();     // 让线程执行
        }
        /// <summary>
        /// 获取时间
        /// </summary>
        private void GetTimes()
        {
            while (true)
            {
                ShowTimes();
                Thread.Sleep(1000);
            }

        }
        /// <summary>
        /// 显示时间
        /// </summary>
        private void ShowTimes()
        {
            if (this.InvokeRequired)
            {
                Action action = new Action(ShowTimes);
                Invoke(action);
            }
            else
            {
                if (IsHandleCreated)
                {
                    Text = CommConst.WelcomsTime + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
                }
            }
        }

        #endregion

        #region 开始计时器

        private void StartTimer()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Enabled = true;   // 激活后就可以触发一个事件
            timer.Interval = 1000;  // 一秒
            timer.AutoReset = true; // 让一个事件引发多次
            timer.Elapsed += new ElapsedEventHandler(ChangedFrmText);
        }

        /// <summary>
        /// 改变窗体的Text值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangedFrmText(object sender, ElapsedEventArgs e)
        {
            if (IsHandleCreated)
            {
                this.BeginInvoke(new Action(() =>
                {
                    Text = CommConst.WelcomsTime + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
                }));
            }
        }

        #endregion

        #region 验证码
        /// <summary>
        /// 验证码
        /// </summary>
        private void InitVerityCode()
        {
            VerifyCode code = new VerifyCode(CommConst.CodeLength);
            imgCode = code.StringCode;
            // 验证码文本框的长度限制
            TxtVerifyCode.MaxLength = CommConst.CodeLength;
            PbVerifyCode.Image = code.CreateImage();
        }

        #endregion

        #region 验证码点击事件
        private void PbVerifyCode_Click(object sender, EventArgs e)
        {
            InitVerityCode();
        }
        #endregion

        #region 登录按钮事件
        /// <summary>
        /// 登录按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            string loginId = TxtLoginId.Text.Trim();
            string loginPwd = TxtLoginPwd.Text.Trim();
            string verifyCode = TxtVerifyCode.Text.Trim();
            if (!CheckInput(loginId, loginPwd, verifyCode))
            {
                return;
            }
            loginPwd = CommDefine.GetMD5Hash(loginPwd);
            // 用户名和密码验证
            CheckLogin(loginId, loginPwd);
        }
        #endregion

        #region 检验用户名和密码

        /// <summary>
        /// 检验用户名和密码
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="loginPwd"></param>
        private void CheckLogin(string loginId, string loginPwd)
        {
            ManagerBLL bll = new ManagerBLL();
            HotelManager hotelManager = bll.GetHotelManager(loginId, CommDefine.GetMD5Hash(loginPwd));
            if (hotelManager is null)
            {
                CommMsgBox.MsgBoxCaveat(CommConst.LoginFail);
                return;
            }

            UserInfo.Id = hotelManager.Id;
            UserInfo.LoginId = hotelManager.LoginId;
            UserInfo.LoginPwd = hotelManager.LoginPwd;

            FrmMain frmMain = new FrmMain();
            frmMain.Show();
            Hide();
        }

        #endregion

        #region 输入验证
        /// <summary>
        /// 输入验证
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="loginPwd"></param>
        /// <returns></returns>
        private bool CheckInput(string loginId, string loginPwd, string verifyCode)
        {
            if (loginId.Length == 0)
            {
                CommMsgBox.MsgBoxCaveat(CommConst.InputFalse);
                TxtLoginId.Focus();
                return false;
            }
            if (loginPwd.Length == 0)
            {
                CommMsgBox.MsgBoxCaveat(CommConst.InputFalse);
                TxtLoginPwd.Focus();
                return false;
            }
            if (!verifyCode.Equals(imgCode))
            {
                CommMsgBox.MsgBoxCaveat(CommConst.VerifyErr);
                TxtVerifyCode.Focus();
                return false;
            }

            return true;
        }

        #endregion


    }
}
